package kr.hkit.jenny.coffee.service;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import kr.hkit.jenny.coffee.dao.CoffeeDao;
import kr.hkit.jenny.coffee.dto.SalesRecord;
import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.powerjava.chapter26.dto.Employee;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class List extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private CoffeeDao dao;
	private ArrayList<SalesRecord> lists;
	private String[][] arLists;
	private Input inputFrame;

	private String[] colNames = { "제품코드", "제품명", "제품단가", "판매수량", "마진율", "판매금액",
			"부가세액", "공급가액", "마진액" };

	/**
	 * Create the frame.
	 */
	public List(CoffeeDao dao, Input inputFrame) {
		this.dao = dao;
		initialize();
		loadData();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);

		table = new JTable(new String[0][0], colNames);
		table.setAutoCreateRowSorter(true);
		scrollPane.setViewportView(table);
	}

	public void loadData() {
		try {
			lists = dao.listItems();
			int len = lists.size();
			arLists = new String[len][colNames.length];
			for (int i = 0; i < len; i++) {
				SalesRecord temp = lists.get(i);
				arLists[i] = temp.toArray();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			TableModel model = new DefaultTableModel(arLists, colNames);
			table.setModel(model);
		}
	}

}
